Imports MyClasses
Imports System.Data.SqlClient
Imports System.Configuration
Public Class DSettings

    Public Shared Function Save(ByRef ObjSettings() As CSettings) As Integer
        Dim dbConnection As New dbAccess
        Dim intResult As Integer = 0
        Dim param(2) As SqlParameter

        Dim StrSql As String = ""
        Try
            dbConnection.BeginTransaction()
            For Each Setting As CSettings In ObjSettings
                StrSql = " Update mUserSettings set KeyValue=@KeyValue  WHERE KeyName=@KeyName AND UserID=@UserID"
                param(0) = New SqlParameter("@KeyValue", Setting.KeyValue)
                param(1) = New SqlParameter("@KeyName", Setting.KeyName)
                param(2) = New SqlParameter("@UserID", Setting.UserID)

                ConfigurationManager.AppSettings(Setting.KeyName) = Setting.KeyValue
                ' ConfigurationManager.AppSettings.Get(Setting.KeyName)
                'intResult = intResult + dbConnection.ExecuteNonQuery(StrSql, param)
                intResult = intResult + dbConnection.ExecuteNonQuery(StrSql, param)
            Next
            If intResult = 0 Then Exit Function

            dbConnection.CommitTransaction()

        Catch ex As Exception
            DGeneral.ErrorHandler(ex.Message, "DAL", "DSettings", "Save", StrSql)
            intResult = 0
        Finally
            If intResult = 0 Then
                dbConnection.RollBackTransaction()
                Save = intResult
            Else : Save = intResult
            End If
            dbConnection.closeSqlcon()
        End Try
    End Function

    Public Shared Function GetSettings() As DataSet
        Try
            Dim StrSql As String

            StrSql = " SELECT  UserId,KeyID,KeyType,KeyValue,KeyDesc,'' AS Description,KeyName " & _
                     " FROM mUserSettings, mCountry WHERE UserId='admin' AND KeyType<>'Country' AND KeyType<>'State' AND KeyType<>'City' " & _
                     " UNION " & _
                     " SELECT  UserId,KeyID,KeyType, KeyValue,KeyDesc,Description,KeyName " & _
                     " FROM mUserSettings, mCountry WHERE UserId='admin' AND mUserSettings.KeyValue=mCountry.countrycd AND KeyType='Country' " & _
                     " UNION " & _
                     " SELECT  UserId,KeyID,KeyType, KeyValue,KeyDesc,Description,KeyName " & _
                     " FROM mUserSettings, mPState WHERE UserId='admin' AND mUserSettings.KeyValue=mPState.statecd AND KeyType='State' " & _
                     " UNION " & _
                     " SELECT  UserId,KeyID,KeyType, KeyValue,KeyDesc,Description,KeyName" & _
                     " FROM mUserSettings, mCity WHERE UserId='admin' AND mUserSettings.KeyValue=mcity.citycd AND KeyType='City' " & _
                     " ORDER BY KeyID "

            '"SELECT UserId,KeyName,KeyType,KeyValue,KeyDesc" & _
            ' " FROM mUserSettings WHERE UserId='admin'"
            Return DbHelper.ExecuteDataSet(StrSql)

        Catch ex As Exception
            DGeneral.ErrorHandler(ex.Message, "DAL", "DSettings", "GetSettings")
            Return Nothing
        End Try
    End Function
    Public Shared Function GetOwnerName(ByVal CounterID As Integer) As String

        Try
            Dim StrSql As String

            StrSql = "select KeyValue from musersettings " & _
                     " where keyType ='Owner_Of_Counter" & CounterID & "' AND UserID='Admin'"
            Return DbHelper.ExecuteScalar(StrSql)


        Catch ex As Exception
            DGeneral.ErrorHandler(ex.Message, "DAL", "DSettings", "GetSettings")
            Return Nothing
        End Try
    End Function
End Class
